﻿/// <reference path="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" />
/// <reference name="MicrosoftAjax.js" />

Type.registerNamespace("LO1200.Web.UI");

LO1200.Web.UI.TwitterFeed = function (element) {
    LO1200.Web.UI.TwitterFeed.initializeBase(this, [element]);
}

LO1200.Web.UI.TwitterFeed.prototype = {
    get_handler: function () {
        if (!this._handler) return "TwitterFeed.ashx";
        return this._handler;
    },
    set_handler: function (value) {
        if (value !== this._handler) {
            this._handler = value;
            this.raisePropertyChanged("handler");
        }
        return this;
    },
    get_query: function () {
        if (!this._query) return null;
        return this._query;
    },
    set_query: function (value) {
        if (value !== this._query) {
            this._query = value;
            this.raisePropertyChanged("query");
        }
        return this;
    },
    get_maxTweets: function () {
        if (!this._maxTweets) return 10;
        return this._maxTweets;
    },
    set_maxTweets: function (value) {
        if (value !== this._maxTweets) {
            this._maxTweets = value;
            this.raisePropertyChanged("maxTweets");
        }
        return this;
    },
    get_interval: function () {
        if (!this._interval) return 1000;
        return this._interval;
    },
    set_interval: function (value) {
        if (value !== this._interval) {
            this._interval = value;
            if (this._timer)
                this._timer.set_interval(value);
            this.raisePropertyChanged("interval");
        }
        return this;
    },
    get_lastID: function () {
        if (!this._lastID) return -1;
        return this._lastID;
    },
    set_lastID: function (value) {
        if (value !== this._lastID) {
            this._lastID = value;
            this.raisePropertyChanged("lastID");
        }
        return this;
    },
    _createTweetElement: function (tweet) {
        if (!tweet) return null;

        return $(document.createElement("li"))
            .addClass("Tweet")
            .append
            (
                $(document.createElement("p"))
                    .addClass("Content")
                    .append
                    (
                        $(document.createElement("a"))
                            .addClass("Author")
                            .attr("href", "#")
                            .html(tweet.AuthorUsername)
                    )
                    .append
                    (
                        $(document.createElement("span"))
                            .addClass("Separator")
                            .html(": ")
                    )
                    .append
                    (
                        $(document.createElement("span"))
                            .addClass("Paragraph")
                            .html(tweet.Content)
                    )
            )
            .append
            (
                $(document.createElement("h3"))
                    .addClass("Date")
                    .html(tweet.Date)
            );
    },
    update: function () {
        this._timer.stop();

        var date = new Date();
        var max = this.get_maxTweets();

        $.ajax({
            url: this.get_handler(),
            data: {
                "q": this.get_query(),
                "max": max,
                "lastID": this.get_lastID(),
                "r": date.valueOf()
            },
            context: this,
            dataType: "json",
            success: function (data) {
                if (data && data.length > 0) {
                    var container = $(this.get_element()).children(".TweetContainer");
                    if (container) {
                        this.set_lastID(data[0].ID);

                        for (var i = data.length - 1; i >= 0; i--) {
                            var element = this._createTweetElement(data[i]);
                            container.prepend(element).children().last().remove();
                        }
                    }
                }
            },
            complete: function () { this._timer.start(); }
        });

        return this;
    },
    initialize: function () {
        this._timer = $create(LO1200.Web.Timer, { id: "Timer_" + this.get_id(), interval: this.get_interval() }, { tick: Function.createDelegate(this, this.update) }, null, null);
        this._timer.start(); // TODO: make this start when the page is completed loading, not before.

        LO1200.Web.UI.TwitterFeed.callBaseMethod(this, 'initialize');
    },
    dispose: function () {
        this._timer.dispose();
        delete this._timer;
        LO1200.Web.UI.TwitterFeed.callBaseMethod(this, 'dispose');
    }
};

LO1200.Web.UI.TwitterFeed.registerClass('LO1200.Web.UI.TwitterFeed', Sys.UI.Control);